<!DOCTYPE html>
<html lang="zh-cn">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>状态管理</title>

    <!-- Bootstrap -->
    <link href="<?php echo PUBLIC_PATH;?>bootstrap/css/bootstrap.min.css" rel="stylesheet">

    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="http://cdn.bootcss.com/html5shiv/3.7.2/html5shiv.min.js"></script>
      <script src="http://cdn.bootcss.com/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
  </head>
  <body>
    <div class="container">
       <h2>LED状态管理</h2>
    <table class="table table-hover">
    <tbody>
      <tr>
        <th>ID</th>
        <th>描述</th>
        <th>状态</th>
        <th>开关</th>
      </tr>

      <?php foreach($data as $rows):?>
        <tr>
          <td><?php echo $rows['id'];?></td>
          <td><?php echo $rows['description'];?></td>
          <td><?php echo $rows['status'];?></td>
          <td>
            <?php if($rows['status']=='off'):?>
            <button type="button" class="btn btn-default switch" data-id="<?php echo $rows['id'];?>" data-status="<?php echo $rows['status'];?>">关闭</button>
            <?php else : ?>
            <button type="button" class="btn btn-primary switch" data-id="<?php echo $rows['id'];?>" data-status="<?php echo $rows['status'];?>">开启</button>
            <?php endif?>
          </td>
        </tr>
      <?php endforeach;?>
    </tbody>
    </table>
    
  </div>





    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="http://cdn.bootcss.com/jquery/1.11.1/jquery.min.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="<?php echo PUBLIC_PATH;?>bootstrap/js/bootstrap.min.js"></script>

    <script type="text/javascript">
      $(document).ready(function(){

        $('.switch').click(function(){
          if($(this).data('status')=='on'){
            $(this).removeClass('btn-primary').addClass('btn-default');
            $(this).data('status','off');
            switchs($(this).data('id'),'off');
          $(this).parent().prev().text('off');
            $(this).text('关闭');
          }else{
            $(this).removeClass('btn-default').addClass('btn-primary');
           switchs($(this).data('id'),'on');
           $(this).parent().prev().text('on');
            $(this).data('status','on');
            $(this).text('开启');
          }

        })
      })

      /*这里进行PUT请求*/
      function switchs(id,action){
        $.ajax({
         async: true,  
         url: 'leds/'+id+'',
         type: 'PUT',
         data: JSON.stringify({status:action}),  
         success: function(response) {
           console.log(response);
         }
        });
      }

    </script>
  </body>
</html>
